package com.xykj.enterprise.wechat.ext.api.externalcontact;

import com.xykj.enterprise.wechat.bean.ext.externalcontact.AddCorpTagVo;
import com.xykj.enterprise.wechat.bean.ext.externalcontact.CorpTagVo;
import com.xykj.enterprise.wechat.ext.service.customer.ExternalContactService;
import com.ydn.appserver.Action;
import com.ydn.appserver.Request;
import com.ydn.appserver.Response;
import com.ydn.appserver.annotations.Function;
import com.ydn.appserver.annotations.Parameter;
import com.ydn.appserver.annotations.Type;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.HashMap;

@Slf4j
@Function(description = "添加企业客户标签", parameters = {
        @Parameter(name = "access_token", type = Type.String, description = "access_token", required = true),

        @Parameter(name = "group_id", type = Type.String, description = "标签组id", required = false),
        @Parameter(name = "group_name", type = Type.String, description = "标签组名称，最长为30个字符", required = false),
        @Parameter(name = "order", type = Type.Integer, description = "标签组次序值。order值大的排序靠前。有效的值范围是[0, 2^32)", required = false),
        @Parameter(name = "tag_name", type = Type.String, description = "添加的标签名称,逗号分隔", required = true),
        @Parameter(name = "agentid", type = Type.Integer, description = "授权方安装的应用agentid。仅旧的第三方多应用套件需要填此参数", required = false),
})
@Component
public class AddCorpTag implements Action {

    @Autowired
    private ExternalContactService externalContactService;

    @Override
    public Response execute(Request request) throws Exception {
        String group_id = request.getString("group_id");
        String group_name = request.getString("group_name");
        Integer order = request.getInteger("order");
        String tag_name = request.getString("tag_name");
        Integer agentid = request.getInteger("agentid");

        HashMap<String, Object> params = new HashMap<String, Object>();
        if (group_id == null && group_name == null) {
            return Response.fail("参数错误！");
        }
        if (group_id != null) {
            params.put("group_id", group_id);
        }
        if (group_name != null) {
            params.put("group_name", group_name);
        }
        if (order != null) {
            params.put("order", order);
        }
        String[] tagSplit = tag_name.split(",");
        ArrayList<CorpTagVo> tagList = new ArrayList<CorpTagVo>();
        for (String tmpTag :
                tagSplit) {
            CorpTagVo tmpCorpTag = new CorpTagVo();
            tmpCorpTag.setName(tmpTag);
            tagList.add(tmpCorpTag);
        }
        params.put("tag", tagList);
        if (agentid != null) {
            params.put("agentid", agentid);
        }

        AddCorpTagVo rsp = externalContactService.addCorpTag(
                request.getString("access_token"),
                params
        );
        if (rsp.getErrcode() != 0) {
            return Response.fail(rsp.getErrmsg());
        }
        return Response.success().put("data", rsp);
    }
}
